import _ from "lodash";

export function RequestMapping(config: { value: string }) {
  return function (target: any, propertyKey: string, descriptor: any) {
    if (!target.$interfaces) {
      target.$interfaces = [];
    }
    if (typeof target[propertyKey] === "function") {
      target.$interfaces.push({
        eventName: config.value,
        callback: target[propertyKey],
        typeKey: propertyKey,
      });
    }
    return descriptor;
  };
}

export function RequestValue(path: string) {
  return function (target: any, propertyKey: string, parameterIndex: number) {
    if (!target.$property) {
      target.$property = {};
    }
    let binds = _.get(target.$property, propertyKey);
    if (!_.isArray(binds)) {
      _.set(target.$property, propertyKey, []);
    }
    _.set(target.$property, `${propertyKey}[${parameterIndex}]`, path);
  };
}

export function SetApi<T extends new (...args: any) => any>(api: string) {
  return function (target: T) {
    target.prototype.$api = api;
  };
}
